home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / Provider.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  10.2 KB  |  338 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Provider.java    1.39 98/10/27
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.io.*;
  18. import java.util.*;
  19.  
  20. /**
  21.  * This class represents a "provider" for the
  22.  * Java Security API, where a provider implements some or all parts of
  23.  * Java Security, including:<ul>
  24.  *
  25.  * <li>Algorithms (such as DSA, RSA, MD5 or SHA-1).
  26.  *
  27.  * <li>Key generation, conversion, and management facilities (such as for
  28.  * algorithm-specific keys).
  29.  *
  30.  * </ul>
  31.  *
  32.  * <p>Each provider has a name and a version number, and is configured
  33.  * in each runtime it is installed in.
  34.  *
  35.  * <p>See <a href =
  36.  * "../../../guide/security/CryptoSpec.html#Provider">The Provider Class</a>
  37.  * in the "Java Cryptography Architecture API Specification & Reference"
  38.  * for information about how providers work and how to install them.
  39.  *
  40.  * @version 1.39 99/03/26
  41.  * @author Benjamin Renaud
  42.  */
  43. public abstract class Provider extends Properties {
  44.  
  45.     /**
  46.      * The provider name.
  47.      *
  48.      * @serial
  49.      */
  50.     private String name;
  51.  
  52.     /**
  53.      * A description of the provider and its services.
  54.      *
  55.      * @serial
  56.      */
  57.     private String info;
  58.  
  59.     /**
  60.      * The provider version number.
  61.      *
  62.      * @serial
  63.      */
  64.     private double version;
  65.  
  66.     /**
  67.      * Constructs a provider with the specified name, version number,
  68.      * and information.
  69.      *
  70.      * @param name the provider name.
  71.      *
  72.      * @param version the provider version number.
  73.      *
  74.      * @param info a description of the provider and its services.
  75.      */
  76.     protected Provider(String name, double version, String info) {
  77.     this.name = name;
  78.     this.version = version;
  79.     this.info = info;
  80.     }
  81.  
  82.     /**
  83.      * Constructs a provider with the specified name. Assigns it
  84.      * version 1.0.
  85.      *
  86.      * @param name the provider name.
  87.      */
  88.     Provider(String name) {
  89.     this(name, 1.0, "no information available");
  90.     }
  91.  
  92.     /**
  93.      * Returns the name of this provider.
  94.      *
  95.      * @return the name of this provider.
  96.      */
  97.     public String getName() {
  98.     return name;
  99.     }
  100.  
  101.     /**
  102.      * Returns the version number for this provider.
  103.      *
  104.      * @return the version number for this provider.
  105.      */
  106.     public double getVersion() {
  107.     return version;
  108.     }
  109.  
  110.     /**
  111.      * Returns a human-readable description of the provider and its
  112.      * services.  This may return an HTML page, with relevant links.
  113.      *
  114.      * @return a description of the provider and its services.
  115.      */
  116.     public String getInfo() {
  117.     return info;
  118.     }
  119.  
  120.     /*
  121.      * Instantiates a provider forom its fully-qualified class name.
  122.      *
  123.      * <p>The assumption is made that providers configured in the
  124.      * security properties file will always be supplied as part
  125.      * of an INSTALLED extension or specified on the class path
  126.      * (and therefore can be loaded using the class loader returned by
  127.      * a call to <code>ClassLoader.getSystemClassLoader</code>, whose
  128.      * delegation parent is the extension class loader for installed
  129.      * extensions).
  130.      *
  131.      * <p>If an applet or application wants to install a provider that is
  132.      * supplied within a BUNDLED extension, it will be able to do so
  133.      * only at runtime, by calling the <code>Security.addProvider</code>
  134.      * method (which is subject to a security check).
  135.      */
  136.     static Provider loadProvider(String name) {
  137.     try {
  138.         ClassLoader cl = ClassLoader.getSystemClassLoader();
  139.         Class provClass;
  140.         if (cl != null) {
  141.         provClass = cl.loadClass(name);
  142.         } else {
  143.         provClass = Class.forName(name);
  144.         }
  145.         Object obj = provClass.newInstance();
  146.         if (obj instanceof Provider) {
  147.         return (Provider)obj;
  148.         } else {
  149.         debug(name + " not a provider");
  150.         }
  151.     } catch (Exception e) {
  152.         debug("error loading provider " + name, e);
  153.     }
  154.     return null;
  155.     }
  156.  
  157.     /**
  158.      * Returns a string with the name and the version number
  159.      * of this provider.
  160.      *
  161.      * @return the string with the name and the version number
  162.      * for this provider.
  163.      */
  164.     public String toString() {
  165.     return name + " version " + version;
  166.     }
  167.  
  168.     /*
  169.      * override the following methods to ensure that provider
  170.      * information can only be changed if the caller has the appropriate
  171.      * permissions.
  172.      */
  173.  
  174.     /**
  175.      * Clears this provider so that it no longer contains the properties
  176.      * used to look up facilities implemented by the provider.
  177.      * 
  178.      * <p>First, if there is a security manager, its <code>checkSecurityAccess</code> 
  179.      * method is called with the string <code>"clearProviderProperties."+name</code>
  180.      * (where <code>name</code> is the provider name) to see if it's ok to clear this provider.
  181.      * If the default implementation of <code>checkSecurityAccess</code> 
  182.      * is used (that is, that method is not overriden), then this results in
  183.      * a call to the security manager's <code>checkPermission</code> method with a
  184.      * <code>SecurityPermission("clearProviderProperties."+name)</code>
  185.      * permission.
  186.      *
  187.      * @throws  SecurityException
  188.      *          if a security manager exists and its <code>{@link
  189.      *          java.lang.SecurityManager#checkSecurityAccess}</code> method denies
  190.      *          access to clear this provider
  191.      *
  192.      * @since JDK1.2
  193.      */
  194.     public synchronized void clear() {
  195.     check("clearProviderProperties."+name);
  196.     super.clear();
  197.     }
  198.  
  199.     /**
  200.      * Reads a property list (key and element pairs) from the input stream.
  201.      *
  202.      * @param      in   the input stream.
  203.      * @exception  IOException  if an error occurred when reading from the
  204.      *               input stream.
  205.      * @see java.util.Properties#load
  206.      */
  207.     public synchronized void load(InputStream inStream) throws IOException {
  208.     check("loadProviderProperties."+name);
  209.     super.load(inStream);
  210.     }
  211.  
  212.     /**
  213.      * Copies all of the mappings from the specified Map to this provider.
  214.      * These mappings will replace any properties that this provider had 
  215.      * for any of the keys currently in the specified Map. 
  216.      *
  217.      * @since JDK1.2
  218.      */
  219.     public synchronized void putAll(Map t) {
  220.     check("putAllProviderProperties."+name);
  221.     super.putAll(t);
  222.     }
  223.  
  224.     /**
  225.      * Returns an unmodifiable Set view of the property entries contained 
  226.      * in this Provider.
  227.      *
  228.      * @see   java.util.Map.Entry
  229.      * @since JDK1.2
  230.      */
  231.     public Set entrySet() {
  232.     return Collections.unmodifiableMap(this).entrySet();
  233.     }
  234.  
  235.     /**
  236.      * Returns an unmodifiable Set view of the property keys contained in 
  237.      * this provider.
  238.      *
  239.      * @since JDK1.2
  240.      */
  241.     public Set keySet() {
  242.     return Collections.unmodifiableMap(this).keySet();
  243.     }
  244.  
  245.     /*
  246.      * Returns an unmodifiable Collection view of the property values 
  247.      * contained in this provider.
  248.      *
  249.      * @since JDK1.2
  250.      */
  251.     public Collection values() {
  252.     return Collections.unmodifiableMap(this).values();
  253.     }
  254.  
  255.     /**
  256.      * Sets the <code>key</code> property to have the specified
  257.      * <code>value</code>.
  258.      * 
  259.      * <p>First, if there is a security manager, its <code>checkSecurityAccess</code> 
  260.      * method is called with the string <code>"putProviderProperty."+name</code>,
  261.      * where <code>name</code> is the provider name,
  262.      * to see if it's ok to set this provider's property values.
  263.      * If the default implementation of <code>checkSecurityAccess</code> 
  264.      * is used (that is, that method is not overriden), then this results in
  265.      * a call to the security manager's <code>checkPermission</code> method with a
  266.      * <code>SecurityPermission("putProviderProperty."+name)</code>
  267.      * permission.
  268.      *
  269.      * @param key the property key.
  270.      *
  271.      * @param value the property value.
  272.      *
  273.      * @return the previous value of the specified property
  274.      * (<code>key</code>), or null if it did not have one.
  275.      *
  276.      * @throws  SecurityException
  277.      *          if a security manager exists and its <code>{@link
  278.      *          java.lang.SecurityManager#checkSecurityAccess}</code> method denies
  279.      *          access to set property values.
  280.      *
  281.      * @since JDK1.2
  282.      */
  283.     public synchronized Object put(Object key, Object value) {
  284.     check("putProviderProperty."+name);
  285.     return super.put(key, value);
  286.     }
  287.  
  288.     /**
  289.      * Removes the <code>key</code> property (and its corresponding
  290.      * <code>value</code>).
  291.      * 
  292.      * <p>First, if there is a security manager, its <code>checkSecurityAccess</code> 
  293.      * method is called with the string <code>""removeProviderProperty."+name</code>,
  294.      * where <code>name</code> is the provider name,
  295.      * to see if it's ok to remove this provider's properties. 
  296.      * If the default implementation of <code>checkSecurityAccess</code> 
  297.      * is used (that is, that method is not overriden), then this results in
  298.      * a call to the security manager's <code>checkPermission</code> method with a
  299.      * <code>SecurityPermission("removeProviderProperty."+name)</code>
  300.      * permission.
  301.      *
  302.      * @param key the key for the property to be removed.
  303.      *
  304.      * @return the value to which the key had been mapped,
  305.      * or null if the key did not have a mapping.
  306.      *
  307.      * @throws  SecurityException
  308.      *          if a security manager exists and its <code>{@link
  309.      *          java.lang.SecurityManager#checkSecurityAccess}</code> method denies
  310.      *          access to remove this provider's properties.
  311.      *
  312.      * @since JDK1.2
  313.      */
  314.     public synchronized Object remove(Object key) {
  315.     check("removeProviderProperty."+name);
  316.     return super.remove(key);
  317.     }
  318.  
  319.     private static void check(String directive) {
  320.          SecurityManager security = System.getSecurityManager();
  321.         if (security != null) {
  322.             security.checkSecurityAccess(directive);
  323.         }
  324.     }
  325.  
  326.     private static void debug(String msg) {
  327.     Security.debug(msg);
  328.     }
  329.  
  330.     private static void debug(String msg, Throwable t) {
  331.     Security.debug(msg, t);
  332.     }
  333.  
  334.     // Declare serialVersionUID to be compatible with JDK1.1
  335.     static final long serialVersionUID = -4298000515446427739L;
  336. }
  337.  
  338.